<?php
/*
Plugin Name: Volaris
Plugin URI: http://code.google.com/p/volaris
Description: A dungeon and dragons character manager for players and game masters.
Version: 2.0.0
Author: Dylan Jacob and Mike Jose
Author URI: http://bubblecore.net
License: GNU GPLv2
*/

class Character {
	
	/**
	* Contains information about the character object.
	*/
	public $info = array(
							'CHARID' => 'NULL',
							'NAME' => 'NULL',
							'PLAYERID' => 'NULL',
							'PLAYERFNAME' => 'NULL',
							'PLAYERLNAME' => 'NULL');
	
	/**
	* List of attributes for a character
	*/
	public $attributes = array(
							'STRENGTH' => 'NULL',
							'AGILITY' => 'NULL',
							'CONSTITUTION' => 'NULL',
							'INTELLECT' => 'NULL',
							'WISDOM' => 'NULL',
							'CHARISMA' => 'NULL',
                                                        'RACE' => 'NULL',
							'CLASS' => 'NULL',
							'LEVEL' => 'NULL',
							'GENDER' => 'NULL',
							'SIZE' => 'NULL',
							'SPEED' => 'NULL' );
	/**
	* Table for strong attack modifiers
	*/
	public $strongattack = array(
	   '1' => '1',
	   '2' => '2',
	   '3' => '3',
	   '4' => '4',
	   '5' => '5',
	   '6' => '6',
	   '7' => '7',
	   '8' => '8',
	   '9' => '9',
	   '10' => '11',
	   '11' => '13',
	   '12' => '15',
	   '13' => '17',
	   '14' => '19',
	   '15' => '21',
	   '16' => '23',
	   '17' => '25',
	   '18' => '27',
	   '19' => '30',
	   '20' => '33'
	);
	
	/**
	* Table for average attack modifiers
	*/
	public $avgattack = array(
	   '1' => '0',
	   '2' => '1',
	   '3' => '2',
	   '4' => '3',
	   '5' => '4',
	   '6' => '4',
	   '7' => '5',
	   '8' => '5',
	   '9' => '6',
	   '10' => '7',
	   '11' => '8',
	   '12' => '8',
	   '13' => '9',
	   '14' => '9',
	   '15' => '11',
	   '16' => '13',
	   '17' => '13',
	   '18' => '17',
	   '19' => '19',
	   '20' => '21'
	);
	
	/**
	* Table for poor attack modifiers
	*/
	public $poorattack = array (
	   '1' => '0',
	   '2' => '1',
	   '3' => '1',
	   '4' => '2',
	   '5' => '2',
	   '6' => '3',
	   '7' => '3',
	   '8' => '4',
	   '9' => '4',
	   '10' => '5',
	   '11' => '5',
	   '12' => '6',
	   '13' => '6',
	   '14' => '7',
	   '15' => '7',
	   '16' => '8',
	   '17' => '8',
	   '18' => '9',
	   '19' => '9',
	   '20' => '11'
	);
	
	/**
	* Table for strong dodge modifiers
	*/
	public $strongdodge = array(
	   '1' => '2',
	   '2' => '3',
	   '3' => '3',
	   '4' => '4',
	   '5' => '4',
	   '6' => '5',
	   '7' => '5',
	   '8' => '6',
	   '9' => '6',
	   '10' => '7',
	   '11' => '7',
	   '12' => '8',
	   '13' => '8',
	   '14' => '9',
	   '15' => '9',
	   '16' => '10',
	   '17' => '10',
	   '18' => '11',
	   '19' => '1',
	   '20' => '12' 
	);
	/**
	* Table for average dodge modifiers
	*/
	public $avgdodge = array(
	   '1' => '1',
	   '2' => '1',
	   '3' => '2',
	   '4' => '2',
	   '5' => '2',
	   '6' => '3',
	   '7' => '3',
	   '8' => '4',
	   '9' => '4',
	   '10' => '4',
	   '11' => '5',
	   '12' => '5',
	   '13' => '6',
	   '14' => '6',
	   '15' => '6',
	   '16' => '7',
	   '17' => '7',
	   '18' => '8',
	   '19' => '8',
	   '20' => '9'  
	);
	
	/**
	* Table for poor dodge modifiers
	*/
	public $poordodge = array(
	   '1' => '0',
	   '2' => '0',
	   '3' => '1',
	   '4' => '1',
	   '5' => '1',
	   '6' => '2',
	   '7' => '2',
	   '8' => '2',
	   '9' => '3',
	   '10' => '3',
	   '11' => '3',
	   '12' => '4',
	   '13' => '4',
	   '14' => '4',
	   '15' => '5',
	   '16' => '5',
	   '17' => '5',
	   '18' => '6',
	   '19' => '6',
	   '20' => '6'
	);           
	
	/**
	* Modifier for strong vitality
	*/
	public $strongvitality = '10';
	/**
	* Modifier for average vitality
	*/
	public $avgvitality = '8';
	/**
	* modifier for poor vitality
	*/
	public $poorvitality = '6';
	/**
	* Modifier for strong skill points
	*/
	public $strongskill = '9';
	/**
	* Modifier for average skill points
	*/
	public $avgskill = '6';
	/**
	* Modifier for poor skill points
	*/
	public $poorskill = '3';

	/**
	*	constructor for the Character class.  Set's $this->info using wp_get_current_user(); and the $wpdb class.
	*/
	function __construct() {
		global $current_user;
		global $wpdb;
		
		$current_user = wp_get_current_user();
		$this->info['PLAYERID'] = $current_user->ID;
		$this->info['PLAYERFNAME'] = $current_user->user_firstname;
		$this->info['PLAYERLNAME'] = $current_user->user_lastname;
		
		$sql = "SELECT CHARID, NAME FROM ". $wpdb->prefix . "CHARACTER WHERE PLAYERID = " . $current_user->ID . ";";
		$querys = $wpdb->get_results($sql);
		
		if ($wpdb->num_rows == "1") {
			foreach($querys as $query) {
				$this->info['CHARID'] = $query->CHARID;
				$this->info['NAME'] = $query->NAME;
			}
		}
		$stats = $this->get_stats($this->info['CHARID']);
		
		$this->attributes['RACE'] = $stats['RACE'];
		$this->attributes['CLASS'] = $stats['CLASS'];
		$this->attributes['LEVEL'] = $stats['LEVEL'];
		$this->attributes['GENDER'] = $stats['GENDER'];
		$this->attributes['SIZE'] = $stats['SIZE'];
		$this->attributes['SPEED'] = $stats['SPEED'];
		
	}
        
        /**
	 * builds the form to add a character
	 * 
	 * @param $character the character object to commit to the DB
	 *
	 * @returns false if there are any database errors, else true.
	 */
        function CreateCharacter($character) {
		global $wpdb;
		
		$sql = "SELECT * FROM " . $wpdb->prefix . "CHARACTER WHERE NAME LIKE '" . $character->info['NAME'] . "';";
		$result = $wpdb->get_results($sql);
		if ($wpdb->num_rows !='0') {
			die("Another user with that character name already exists.");
		} else {
			$chardata = array(		'CHARID' 	=> 'NULL',
							'NAME' 		=> $character->info['NAME'],
							'PLAYERID'	=> $character->info['PLAYERID'] );
			#var_dump($chardata);
                        #var_dump($character->info);
                        $wpdb->insert($wpdb->prefix . "CHARACTER", $chardata);
                        var_dump($wpdb->last_result);
                        #echo "<br />";
                        $charid = $wpdb->insert_id;
			foreach($character->attributes as $key => $val) {
				$metadata =  array(
								'METAID'	=> 'NULL',
								'CHARID'	=> $charid,
								'META_KEY'	=> $key,
								'META_VALUE'	=> $val );
                                #var_dump($metadata);
                                $wpdb->insert($wpdb->prefix . "CHAR_META", $metadata);
                                var_dump($wpdb->last_result);
			}
                        #var_dump($character->attributes);
                        #var_dump($metadata);
			
		}
	}
        
	/**
	 * \deprecated This function is deprecated and not to be used.  Use instead <code>CreateCharacter</code>
         * creates a new character using the wordpress WPDB class to INSERT a row into the CHARACTER table
	 *
	 * @param $args array containing elements that directly map to columns in the CHARACTER table.
	 **/
	function new_character($args) {
		global $wpdb;
		
		$sql = "INSERT INTO " . $wpdb->prefix . "CHARACTER (CHARID, NAME, PLAYERID) VALUES ('" . $args[CHARID] . "', '" . $args[NAME] . "', '" . $args[PLAYERID] . "');";
		$result = $wpdb->query($sql);
		echo $result;
		if (!$result) {
			die("There are too many hefalumps to count!");
		}
	}
	
	/**
	* Gets information about the character that is stored in the CHARACTER table.
	* May not even need this function since we load $this->info  as a publicly accessible variable containing this and more information.
	*
	* @param $charid an integer value denoting the Character's Primary Key value
	*/
	function get_info($charid) {
		global $wpdb;
		
		$sql = "SELECT * FROM " . $wpdb->prefix . "CHARACTER WHERE CHARID = ". $charid . ";";
		$querys = $wpdb->get_results($sql);
		
		foreach($querys as $query) {
			$result = array( "CHARID" => $query->CHARID, "NAME" => $query->NAME, "PLAYERID" => $query->PLAYERID);
		}
		return $result;
	}
	
	/**
	* set the Player (wordpress user) that owns this character.
	*
	* @param $playerid an integer value denoting the ID of the user
	*/
	function set_player($playerid) {
	
	}
	
	/**
	* get the id of the user who owns this Character
	*
	* @param $charid an integer value denoting the Character's Primary Key Value
	*
	* @returns the ID of the player's user account.  If there is more than one row found returns false.
	*/
	function get_player($charid) {
		global $wpdb;
		
		$sql = "SELECT PLAYERID FROM " . $wpdb->prefix . "CHARACTER WHERE CHARID = " . $charid . ";";
		$querys = $wpdb->get_results($sql);
		
		if ($wpdb->num_rows == "1") {
			foreach($querys as $query) {
				$result = $query->PLAYERID;
			}
			return $result;
		} else {
			return false;
		}
	}
	
	/**
	* Make the Character a member of a specific party
	*
	* @param $charid integer value of the character
	* @param $playerid integer value of the party
	*/
	function set_party($charid, $playerid) {
	
	}
	
	/**
	* Automates the process of a character leveling up
	*
	* @param $charid integer value of the character
	*/
	function level_up($charid) {
	
	}
	
	/**
	* Adds experience points to the Character
	*
	* @param $charid integer value of the character
	*/
	function add_xp($charid) {
	
	}
	
	/**
	* Performs a currency transaction involving the Character's inventory
	* 
	* @param $charid integer value of the Character
	* @param $amount This should be the amount of Silver the Character pays, or receives.  This number can be positive, or negative based on whether currency is entering or leaving the Character's coin purse.
	*/
	function transaction($charid, $amount) {
	
	}
	
	/**
	* Attaches an item to the Character's equipment slot.
	*
	* @param $charid integer value of the character
	* @param $baseid base id of the item
	* @param $slot address of the destination equipment slot
	*/
	function equip_item($charid, $baseid, $slot) {
	
	}

	/**
	* detaches an item from the Character's equipment slot.
	*
	* @param $charid integer value of the character
	* @param $baseid base id of the item
	* @param $slot address of the source equipment slot
	*/
	function unequip_item($charid, $baseid, $slot) {
	
	}
	
	/**
	* Sets a particular statistic of the character to a given value
	*
	* @param $charid integer value of the Character
	* @param $statkey identifier for the stat to be set ($key)
	* @param $statval value the stat should be set to ($val)
	*/
	function set_stat($charid, $statkey, $statval) {
	
	}
        
        	
	/**
	 * Gets all statistics of the character designated by <code>$charid</code>
	 * 
	 *
	 * @param $charid integer value of the Character
	 *
         * @returns an associative array with $key => $val pairs
	 */
	function get_stats($charid) {
            global $wpdb;
            
            $sql = "SELECT META_KEY, META_VALUE FROM ". $wpdb->prefix ."CHAR_META WHERE CHARID = " . $charid . ";";
            $results = $wpdb->get_results($sql);
            foreach($results as $result) {
                $key = $result->META_KEY;
                $val = $result->META_VALUE;
                $array[$key] = $val;
            }
            
            return $array;
	
	}
	
	/**
	* Sets a profession to a given string
	*
	* @param $charid integer value for a character
	* @param $profession string value to be set as the profession
	* @param $professionid non-zero integer value identifying which profession field should be changed
	* @param $professionskill the skill level that the character has in that profession
	*/
	function set_profession($charid, $profession, $professionid, $professionskill) {
	
	}
	
	function set_knowledge() {
	
	}
	
	function change_VP() {
	
	}
	
	function change_WP() {
	
	}
	
	function change_SP() {
	
	}
	
	function change_Mana() {
	
	}
	
	function inventory_add() {
	
	}
	
	function inventory_remove() {
	
	}
	
	/**
	*This function takes a given value and calculates the modifier using the formula <code>modifier = (score-10)/2</code>
	*
	*@param $value the base skill that is to be converted to modifier
	*
	*@returns a numerical value that represents the associated modifier
	*/
	function calculate_modifier($value) {
		$mod = floor(($value-10)/2);
		return $mod;		
	}
        
	/**
	 *This function takes a given value and calculates the class modifier (which is different from a normal modifier.  The formula is <code>class modifier= class level + 4</code>.
	 * 
	 * @param $character a <code>Character</code> object
         * @param $discipline the discipline to calculate the classmod for (i.e. <code>attack</code> for base attack class mod, <code>dodge</code> for doge class mod
	 * 
	 * @returns a numerical value that represents the class modifier for a given discipline. 
	 */
	function calculate_classmod($character,$discipline) {
		$level = $this->attributes['LEVEL'] - 1;
		
		if ($discipline == "ATTACK") {
			if ($this->attributes['CLASS'] == "Monk" || $this->attributes['CLASS'] == "Ranger" || $this->attributes['CLASS'] == "Rogue") {
				$classmod = $this->strongattack[$level];
			}
			if ($this->attributes['CLASS'] == "Druid" || $this->attributes['CLASS'] == "Knight" || $this->attributes['CLASS'] == "Warrior") {
				$classmod = $this->avgattack[$level];
			}
			if ($this->attributes['CLASS'] == "Cleric" || $this->attributes['CLASS'] == "Mage") {
				$classmod = $this->poorattack[$level];
			}
		}
		if ($discipline == "DODGE") {
			if ($this->attributes['CLASS'] == "Knight" || $this->attributes['CLASS'] == "Ranger" || $this->attributes['CLASS'] == "Warrior") {
				$classmod = $this->strongdodge[$level];
			}
			if ($this->attributes['CLASS'] == "Druid" || $this->attributes['CLASS'] == "Monk" || $this->attributes['CLASS'] == "Rogue") {
				$classmod = $this->avgdodge[$level];
			}
			if ($this->attributes['CLASS'] == "Cleric" || $this->attributes['CLASS'] == "Mage") {
				$classmod = $this->poordodge[$level];
			}
		}
		if ($discipline == "VITALITY") {
			if ($this->attributes['CLASS'] == "Knight" || $this->attributes['CLASS'] == "Monk" || $this->attributes['CLASS'] == "Warrior") {
				$classmod = $this->strongvitality;
			}
			if ($this->attributes['CLASS'] == "Druid" || $this->attributes['CLASS'] == "Ranger") {
				$classmod = $this->avgvitality;
			}
			if ($this->attributes['CLASS'] == "Cleric" || $this->attributes['CLASS'] == "Mage" || $this->attributes['CLASS'] == "Rogue") {
				$classmod = $this->poorvitality;
			}
		}
		if ($discipline == "SKILL") {
			if ($this->attributes['CLASS'] == "Cleric" || $this->attributes['CLASS'] == "Mage" || $this->attributes['CLASS'] == "Rogue") {
				$classmod = $this->strongskill;
			}
			if ($this->attributes['CLASS'] == "Knight" || $this->attributes['CLASS'] == "Ranger") {
				$classmod = $this->avgskill;
			}
			if ($this->attributes['CLASS'] == "Druid" || $this->attributes['CLASS'] == "Monk" || $this->attributes['CLASS'] == "Warrior") {
				$classmod = $this->poorskill;
			}
		}
		return $classmod;
	}
}

class Campaign {
	function new_campaign($name) {
	
	}
	
	function get_partys() {
	
	}
	
	function get_party() {
	
	}
}

class Party {
	function new_party($name) {
		
	}
	
	function get_characters() {
	
	}
	
	function get_character() {
	
	}
	
	function set_campaign() {
	
	} 
}

class Player {
#DEPRECATED
	function get_character($playerid) {
		//returns the character id of the player's character
		global $wpdb;
		
		$sql = "SELECT CHARID FROM ". $wpdb->prefix . "CHARACTER WHERE PLAYERID = " . $playerid . ";";
		$querys = $wpdb->get_results($sql);
		
		if ($wpdb->num_rows == "1") {
			foreach($querys as $query) {
				$result = $query->CHARID;
			}
			return $result;
		} else {
			return false;
		}
	}

}

class CharacterSheet {

	function build_info($character) {
	
		echo "
	<div class=\"col5\">\n
		<div class=\"col\">\n
			<div class=\"field bold\">\n
				" . $character->info['NAME'] . "\n
			</div>\n
			<label for=\"charactername\">Character Name</label>\n
		</div>\n
		<div class=\"clear\"></div>\n
		<div class=\"col25\">\n
			<div class=\"field bold\">\n
				" . $character->attributes['CLASS'] . "\n
			</div>\n
			<label for=\"characterclass\">Class</label>\n
		</div>\n
		<div class=\"col25\">\n
			<div class=\"field bold\">\n
				" . $character->attributes['LEVEL'] . "\n
			</div>\n
			<label for=\"characterlvl\">Level</label>\n
		</div>\n
		<div class=\"col5\">\n
			<div class=\"field bold\">\n
				" . $character->attributes['RACE'] . "\n
			</div>\n
			<label for=\"characterrace\">Race</label>\n
		</div>\n
		<div class=\"clear\"></div>\n
		<div class=\"col25\">\n
			<div class=\"field bold\">\n
				" . $character->attributes['GENDER'] . "\n
			</div>\n
			<label for=\"charactergender\">Gender</label>\n
		</div>\n
		<div class=\"col25\">\n
			<div class=\"field bold\">\n
				" . $character->attributes['SIZE'] . "\n
			</div>\n
			<label for=\"charactersize\">Size</label>\n
		</div>\n
		<div class=\"col25\">\n
			<div class=\"field bold\">\n
				" . $character->attributes['SPEED'] . "\n
			</div>\n
			<label for=\"characterspeed\">Speed</label>\n
		</div>\n
		<div class=\"col25\"></div>\n
	</div>\n
	<div class=\"col5\">\n
		<div class=\"col\">\n
			<div class=\"field bold\">\n
				" . $character->info['PLAYERFNAME'] . " " . $character->info['PLAYERLNAME'] . "\n
			</div>\n
			<label for=\"playername\">Player Name</label>\n
		</div>\n
		<div class=\"col100\">\n
			<div class=\"field bold\">
				<input type=\"text\" id=\"characterfaction\" class=\"W100pc\" name=\"characterfaction\" value=\"\" />\n
			</div>\n
			<label for=\"characterfaction\">Faction</label>\n
		</div>\n
	</div>\n";
	}
	
	function build_health($character) {
		$stats = $character->get_stats($character->info['CHARID']);
		echo "
	<div class=\"col6\">\n
		<label class=\"biglabel\" for=\"vp\">VP<br><small>Vitality Points</small></label>\n
		<div id=\"vp\">\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"vptotal\">\n
				</div>\n
				<label class=\"labelsmall\" for=\"vptotal\">Total</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">=</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"vpclassmodifier\">\n
					" . $character->calculate_classmod($character,"VITALITY") . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"vpclassmodifier\">Class Mod</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">+</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"vpconstitutionmodifier\">\n
					" . $character->calculate_modifier($stats['CONSTITUTION']) . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"vpconstitutionmodifier\">Const Mod</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">x</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"vpclasslevel\">\n
                    " . $stats['LEVEL'] . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"vpclasslevel\">Class Level</label>\n
			</div>\n
			<div class=\"clear\"></div>\n
			<div class=\"col25\">\n
				<div class=\"current fieldsmall\">\n
					<span class=\"current-vitality\"></span>\n
				</div>\n
				<label class=\"labelsmall\" for=\"vpcurrent\">Current</label>\n
			</div>
				<div class=\"col6\">\n
						<div class=\"fieldsmall\">\n
							<div class=\"col25\"><button id=\"vitality-1\" class=\"button-primary alignright\">-1</button></div>\n
							<div class=\"col25\"><button id=\"vitality-5\" class=\"button-primary alignright\">-5</button></div>\n
							<div class=\"col25\"><button id=\"vitality-10\" class=\"button-primary alignright\">-10</button></div>\n
							<div class=\"col25\"><button id=\"vitality-reset\" class=\"button-primary alignright\">Reset</button></div>\n
						</div>\n
				</div>
		</div>\n
		<div class=\"clear\"></div>\n
		<label class=\"biglabel\" for=\"wp\">WP<br><small>Wound Points</small></label>\n
		<div id=\"wp\">\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"wptotal\">\n
				</div>\n
				<label class=\"labelsmall\" for=\"wptotal\">Total</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">=</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"wpconstitution\">\n
					" . $stats['CONSTITUTION'] . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"wpconstscore\">Const Score</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">+</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"wpmiscmod\">\n";
                    if(!isset($stats['MISCMOD'])) { echo "0"; } else { echo $stats['MISCMOD']; }
                    echo "
				</div>\n
				<label class=\"labelsmall\" for=\"wpmiscmod\">Misc. Mod</label>\n
			</div>\n
			<div class=\"clear\"></div>\n
			<div class=\"col25\">\n
				<div class=\"current fieldsmall\">\n
					<span class=\"current-wound\"></span>\n
				</div>\n
				<label class=\"labelsmall\" for=\"wpcurrent\">Current</label>\n
			</div>\n
				<div class=\"col6\">\n
						<div class=\"fieldsmall\">\n
							<div class=\"col25\"><button id=\"wound-1\" class=\"button-primary alignright\">-1</button></div>\n
							<div class=\"col25\"><button id=\"wound-5\" class=\"button-primary alignright\">-5</button></div>\n
							<div class=\"col25\"><button id=\"wound-10\" class=\"button-primary alignright\">-10</button></div>\n
							<div class=\"col25\"><button id=\"wound-reset\" class=\"button-primary alignright\">Reset</button></div>\n
						</div>\n
				</div>\n
		</div>\n
		<div class=\"clear\"></div>\n
		<label class=\"biglabel\" for=\"sp\">SP<br><small>Subdual Points</small></label>\n
		<div id=\"sp\">\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"sptotal\">\n
				</div>\n
				<label class=\"labelsmall\" for=\"sptotal\">Total</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">=</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"spclassmodifier\">\n
					" . $character->calculate_classmod($character,"VITALITY") . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"spclassmodifier\">Class Mod</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">+</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"spconstitutionmodifier\">\n
					" . $character->calculate_modifier($stats['CONSTITUTION']) . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"spconstitutionmodifier\">Const Mod</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">x</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"spclasslevel\">\n
					" . $stats['LEVEL'] . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"spclasslevel\">Class Level</label>\n
			</div>\n
			<div class=\"clear\"></div>\n
			<div class=\"col25\">\n
				<div class=\"current fieldsmall\">\n
					<span class=\"current-subdual\"></span>\n
				</div>\n
				<label class=\"labelsmall\" for=\"spcurrent\">Current</label>\n
			</div>\n
				<div class=\"col6\">\n
						<div class=\"fieldsmall\">\n
							<div class=\"col25\"><button id=\"subdual-1\" class=\"button-primary alignright\">-1</button></div>\n
							<div class=\"col25\"><button id=\"subdual-5\" class=\"button-primary alignright\">-5</button></div>\n
							<div class=\"col25\"><button id=\"subdual-10\" class=\"button-primary alignright\">-10</button></div>\n
							<div class=\"col25\"><button id=\"subdual-reset\" class=\"button-primary alignright\">Reset</button></div>\n
						</div>\n
				</div>\n
		</div>\n
		<div class=\"clear\"></div>\n
		<label class=\"biglabel\" for=\"mp\">MP<br><small>Mana</small></label>\n
		<div id=\"mp\">\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"mptotal\">\n
				</div>\n
				<label class=\"labelsmall\" for=\"mptotal\">Total</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">=</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"mpclassmodifier\">\n
					" . $character->calculate_classmod($character,"VITALITY") . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"mpclassmodifier\">Class Mod</label>\n
			</div>\n
			<div class=\"col05 fieldsmall\">x</div>\n
			<div class=\"col10\">\n
				<div class=\"fieldsmall\" id=\"mpclasslevel\">\n
					" . $stats['LEVEL'] . "\n
				</div>\n
				<label class=\"labelsmall\" for=\"mpclasslevel\">Class Level</label>\n
			</div>\n
			<div class=\"clear\"></div>\n
			<div class=\"col25\">\n
				<div class=\"current fieldsmall\">\n
					<span class=\"current-mana\"></span>\n
				</div>\n
				<label class=\"labelsmall\" for=\"mpcurrent\">Current</label>\n
			</div>\n
				<div class=\"col6\">\n
						<div class=\"fieldsmall\">\n
							<div class=\"col25\"><button id=\"mana-1\" class=\"button-primary alignright\">-1</button></div>\n
							<div class=\"col25\"><button id=\"mana-5\" class=\"button-primary alignright\">-5</button></div>\n
							<div class=\"col25\"><button id=\"mana-10\" class=\"button-primary alignright\">-10</button></div>\n
							<div class=\"col25\"><button id=\"mana-reset\" class=\"button-primary alignright\">Reset</button></div>\n
						</div>\n
				</div>\n
	</div>\n";
	}
	
	function build_abilities($character) {
		$stats = $character->get_stats($character->info['CHARID']);
		echo "
			<div class=\"clear\"></div>
			<div class=\"col75\">\n
				<div class=\"small-head\">\n
					<!--<div class=\"col05 aligncenter\">&nbsp;</div>\n-->
					<div class=\"col17 aligncenter\"><small>Ability Name</small></div>\n
					<div class=\"col17 aligncenter\"><small>Ability Score</small></div>\n
					<div class=\"col17 aligncenter\"><small>Ability Modifier</small></div>\n
					<div class=\"col17 aligncenter\"><small>Temp. Score</small></div>\n
					<div class=\"col17 aligncenter\"><small>Temp. Modifier</small></div>\n
				</div>\n
				<div class=\"clear\"></div>
				<div id=\"str\">\n
					<label class=\"biglabel\" for=\"str\">STR<br><small>Strength</small></label>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"strscore\"> " . $stats['STRENGTH'] . "</div>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"strmod\"> " . $character->calculate_modifier($stats['STRENGTH']) . "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"strtmpscore\">"; 
					if (isset($stats['strengthtmp'])) {	echo $stats['strengthtmp']; } else { echo "&nbsp;"; }
					echo "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"strtmpmod\">"; 
					if (isset($stats['strengthtmpmod'])) { echo $stats['strengthtmpmod']; } else { echo "&nbsp;"; }
					echo "</div>\n
				</div>\n
				<div class=\"clear\"></div>
				<div id=\"agi\">\n
					<label class=\"biglabel\" for=\"agi\">AGI<br><small>Agility</small></label>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"agiscore\"> " . $stats['AGILITY'] . "</div>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"agimod\"> " . $character->calculate_modifier($stats['AGILITY']) . "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"agitmpscore\">"; 
					if (isset($stats['agilitytmp'])) {	echo $stats['agilitytmp']; } else { echo "&nbsp;"; }
					echo "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"agitmpmod\">"; 
					if (isset($stats['agilitytmpmod'])) { echo $stats['agilitytmpmod']; } else { echo "&nbsp;"; }
					echo "</div>\n
				</div>\n
				<div class=\"clear\"></div>
				<div id=\"con\">\n
					<label class=\"biglabel\" for=\"con\">CON<br><small>Constitution</small></label>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"conscore\"> " . $stats['CONSTITUTION'] . "</div>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"conmod\"> " . $character->calculate_modifier($stats['CONSTITUTION']) . "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"contmpscore\">"; 
					if (isset($stats['consttmp'])) {	echo $stats['consttmp']; } else { echo "&nbsp;"; }
					echo "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"contmpmod\">"; 
					if (isset($stats['consttmpmod'])) { echo $stats['consttmpmod']; } else { echo "&nbsp;"; }
					echo "</div>\n
				</div>\n
				<div class=\"clear\"></div>
				<div id=\"int\">\n
					<label class=\"biglabel\" for=\"int\">INT<br><small>Intellect</small></label>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"intscore\"> " . $stats['INTELLECT'] . "</div>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"intmod\"> " . $character->calculate_modifier($stats['INTELLECT']) . "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"inttmpscore\">"; 
					if (isset($stats['intellecttmp'])) {	echo $stats['intellecttmp']; } else { echo "&nbsp;"; }
					echo "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"inttmpmod\">"; 
					if (isset($stats['intellecttmpmod'])) { echo $stats['intellecttmpmod']; } else { echo "&nbsp;"; }
					echo "</div>\n
				</div>\n
				<div class=\"clear\"></div>
				<div id=\"wis\">\n
					<label class=\"biglabel\" for=\"wis\">WIS<br><small>Wisdom</small></label>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"wisscore\"> " . $stats['WISDOM'] . "</div>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"wismod\"> " . $character->calculate_modifier($stats['WISDOM']) . "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"wistmpscore\">"; 
					if (isset($stats['wisdomtmp'])) {	echo $stats['wisdomtmp']; } else { echo "&nbsp;"; }
					echo "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"wistmpmod\">"; 
					if (isset($stats['wisdomtmpmod'])) { echo $stats['wisdomtmpmod']; } else { echo "&nbsp;"; }
					echo "</div>\n
				</div>\n
				<div class=\"clear\"></div>
				<div id=\"cha\">\n
					<label class=\"biglabel\" for=\"cha\">CHA<br><small>Charisma</small></label>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"chascore\"> " . $stats['CHARISMA'] . "</div>\n
					<div class=\"col17 fieldsmall border shade-grey\" id=\"chamod\"> " . $character->calculate_modifier($stats['CHARISMA']) . "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"chatmpscore\">"; 
					if (isset($stats['charismatmp'])) {	echo $stats['charismatmp']; } else { echo "&nbsp;"; }
					echo "</div>\n
					<div class=\"col17 fieldsmall border shade-orange\" id=\"chatmpmod\">"; 
					if (isset($stats['charismatmpmod'])) { echo $stats['charismatmpmod']; } else { echo "&nbsp;"; }
					echo "</div>\n
				</div>\n
				<div class=\"clear\"></div>
			</div>\n
			<div class=\"clear\"></div>\n
			";
	}
	
	function build_character_sheet() {
#		$player = new Player();
		$character = new Character();
		echo "<fieldset class=\"TableLike\">\n<div class=\"col100\">\n";
		$this->build_info($character);
		echo "	</div>\n
			<div class=\"clear\"></div>\n
				<div class=\"col6\">\n";
		$this->build_abilities($character);
		echo "</div>\n
		<div class=\"col4\">\n";
		$this->build_health($character);
		echo "</div>\n";
	}
}

class Administration {
	/*
	* builds the form to add a character
	* 
	* @param $character the character object to commit to the DB
	*
	* @returns false if there are any database errors, else true.
	*/
	function CreateCharacter($character) {
		global $wpdb;
		
		$sql = "SELECT * FROM " . $wpdb->prefix . "CHARACTER WHERE NAME LIKE '" . $character->attributes['NAME'] . "';";
		$result = $wpdb->getresults($sql);
		if ($wpdb->num_rows !='0') {
			die("Another user with that character name already exists.");
		} else {
			$chardata = array(		'CHARID' 	=> 'NULL',
							'NAME' 		=> $character->attributes['NAME'],
							'PLAYERID'	=> $character->info['PLAYERID'] );
			$wpdb->insert($wpdb->prefix . "CHARACTER", $chardata);
			foreach($character->attributes as $key => $val) {
				$metadata =  array(
								'METAID'	=> 'NULL',
								'CHARID'	=> $wpdb->insert_id,
								'META_KEY'	=> $key,
								'META_KEY'	=> $val );
			}
			$wpdb->insert($wpdb->prefix . "CHAR_META", $metadata);
		}
	}
}

class Pages {
    /**
     * Builds the character administration page
     * 
     * Uses global $wpdb
     */
    function character_page() {
        global $wpdb;
        if ($_POST['action']=="create") {
            $character = new Character();
            $character->info['NAME'] = $_POST['charactername'];
            $character->info['PLAYERID'] = $_POST['player'];
            $character->attributes['CLASS'] = $_POST['characterclass'];
            $character->attributes['LEVEL'] = $_POST['characterlevel'];
            $character->attributes['RACE'] = $_POST['characterrace'];
            $character->attributes['GENDER'] = $_POST['charactergender'];
            $character->attributes['SIZE'] = $_POST['charactersize'];
            $character->attributes['SPEED'] = $_POST['characterspeed'];
            $character->attributes['STRENGTH'] = $_POST['strength'];
            $character->attributes['AGILITY'] = $_POST['agility'];
            $character->attributes['CONSTITUTION'] = $_POST['constitution'];
            $character->attributes['INTELLECT'] = $_POST['intellect'];
            $character->attributes['WISDOM'] = $_POST['wisdom'];
            $character->attributes['CHARISMA'] = $_POST['charisma'];
            
            /*var_dump($character->info);
            echo "<br /><br />";
            var_dump($character->attributes);
            echo "<br /><br />";
            var_dump($_POST);*/
            
            $character->CreateCharacter($character);
        } else { 
            echo "<div class=\"wrap\">\n
                <h2>Character Creator</h2>\n
                <form name=\"charinfo\" method=\"post\" action=\"#\">\n
                    <input type=\"hidden\" value=\"\" name=\"something\">\n
                    <input type=\"hidden\" value=\"create\" name=\"action\">\n
                    <div id=\"charinfo\">
                        <table class=\"form-table\">\n
                                <tbody>\n
                                    <tr valign=\"top\">
                                        <th scope=\"row\">
                                            <label for=\"charactername\">Character Name:</label>\n                           
                                        </th>\n
                                        <td>\n
                                            <input id=\"charactername\" class=\"regular-text\" type=\"text\" value=\"\" name=\"charactername\">\n
                                        </td>\n
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"playerid\">Player:</label>\n
                                        </th>\n
                                        <td>\n
                                            <select name=\"player\" id=\"player\">\n
                                                <option value=\"\" selected=\"selected\">Select a User</option>\n";
                    $sql = "SELECT * FROM " . $wpdb->users . ";";
                    $results = $wpdb->get_results($sql);
                    foreach($results as $result) {
                        if ($result->ID != "1") {
                            echo "<option value=\"" . $result->ID . "\">" . $result->display_name . "</option>\n";
                        }
                    }
                    echo "                  </select>\n
                                        </td>\n
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"characterclass\">Class:</label>\n
                                        </th>\n
                                        <td>\n
                                            <input id=\"characterclass\" class=\"regular-text\" type=\"text\" value=\"\" name=\"characterclass\">\n
                                        </td>
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"characterlevel\">Level:</label>\n
                                        </th>\n
                                        <td>
                                            <select name=\"characterlevel\" id=\"characterlevel\">\n";
                    for ($i=1;$i <= '20'; $i++) {
                            echo "<option value=\"" . $i . "\">" . $i ."</option>\n";
                    }
                    echo "                  </select>\n
                                        </td>\n
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"characterrace\">Race:</label>\n
                                        </th>\n
                                        <td>\n
                                            <select name=\"characterrace\" id=\"characterrace\">\n
                                                <option value=\"\" selected=\"selected\">Select a Race</option>\n
                                                <option value=\"Human\">Human</option>\n
                                                <option value=\"Celadrin\">Celadrin</option>\n
                                                <option value=\"Dwarf\">Dwarf</option>\n
                                                <option value=\"Firesoul\">Firesoul</option>\n
                                                <option value=\"Icesoul\">Icesoul</option>\n
                                                <option value=\"NightElf\">Night Elf</option>\n
                                                <option value=\"Orc\">Orc</option>\n
                                                <option value=\"Rapax\">Rapax</option>\n
                                                <option value=\"Sun Elf\">Sun Elf</option>\n
                                                <option value=\"Windsoul\">Windsoul</option>\n
                                            </select>\n
                                        </td>\n
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"charactergender\">Gender:</label>\n
                                        </th>\n
                                        <td>\n
                                            <input type=\"radio\" name=\"charactergender\" value=\"male\" id=\"charactergender\" />Male\n
                                            <input type=\"radio\" name=\"charactergender\" value=\"female\" id=\"charactergender\" />Female\n
                                        </td>\n
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"charactersize\">Size:</label>\n
                                        </th>\n
                                        <td>\n
                                            <input id=\"charactersize\" class=\"regular-text\" type=\"text\" name=\"charactersize\">\n
                                        </td>\n
                                    </tr>\n
                                    <tr valign=\"top\">\n
                                        <th scope=\"row\">\n
                                            <label for=\"characterspeed\">Speed:</label>\n
                                        </th>\n
                                        <td>\n
                                            <input id=\"characterspeed\" class=\"regular-text\" type=\"text\" name=\"characterspeed\">\n
                                        </td>\n
                                    </tr>\n
                                </tbody>\n
                        </table>\n
                    </div>\n
                    <div id=\"charabilities\">\n
                        <table>\n
                            <tbody>\n
                                <tr valign=\"top\">
                                    <th scope=\"row\">\n
                                        <label for=\"strength\">Strength:</label>\n
                                    </th>\n
                                    <td>\n
                                        <input id=\"strength\" class=\"regular-text\" type=\"text\" id=\"strength\" name=\"strength\">\n
                                    </td>\n
                                </tr>\n
                                <tr valign=\"top\">\n
                                    <th scope=\"row\">\n
                                        <label for=\"agility\">Agility:</label>\n
                                    </th>\n
                                    <td>\n
                                        <input id=\"agility\" class=\"regular-text\" type=\"text\" id=\"agility\" name=\"agility\">\n
                                    </td>\n
                                </tr>\n
                                <tr valign=\"top\">\n
                                    <th scope=\"row\">\n
                                        <label for=\"constitution\">Constitution:</label>\n
                                    </th>\n
                                    <td>\n
                                        <input name=\"constitution\" class=\"regular-text\" type=\"text\" name=\"constitution\">\n
                                    </td>\n
                                </tr>\n
                                <tr valign=\"top\">\n
                                    <th scope=\"row\">\n
                                        <label for=\"intellect\">Intellect:</label>\n
                                    </th>\n
                                    <td>\n
                                        <input name=\"intellect\" class=\"regular-text\" type=\"text\" name=\"intellect\">
                                    </td>\n
                                </tr>\n
                                <tr valign=\"top\">\n
                                    <th scope=\"row\">\n
                                        <label for=\"wisdom\">Wisdom:</label>\n
                                    </th>\n
                                    <td>\n
                                        <input id=\"wisdom\" class=\"regular-text\" type=\"text\" name=\"wisdom\">
                                    </td>\n
                                </tr>\n
                                <tr valign=\"top\">\n
                                    <th scope=\"row\">\n
                                        <label for=\"charisma\">Charisma:</label>\n
                                    </th>\n
                                    <td>\n
                                        <input id=\"charisma\" class=\"regular-text\" type=\"text\" name=\"charisma\">\n
                                    </td>\n
                                </tr>\n
                            </tbody>\n
                        </table>
                    </div>\n 
                    <div id=\"charparty\">\n
                        <table>\n
                            <tr valign=\"top\">\n
                                <th scope=\"row\">\n
                                    <labelfor=\"party\">Party:</label>\n
                                </th>\n
                                <td>\n
                                    <select name=\"party\" id=\"party\">\n
                                        <option value=\"\">Select Party</option>\n";
                                    $parties=get_parties();
                                    foreach($parties as $party) {
                                            echo "<option value=\"" . $partyid['PARTYID'] . "\">" . $party['NAME'] . "</option>\n";
                                    }
                    echo"           </select>\n
                                </td>\n
                            </tr>\n
                        </table>\n
                    </div>\n
                    </form>\n
                    <div class=\"clear\"></div>\n";
                    echo "<div class=\"col2\">\n" . submit_button(__('Previous'),'secondary','previous', false) . "</div>\n<div class=\"col2\">" . submit_button(__('Previous'),'secondary','previous2', false) . "</div>\n<div class=\"col2\">\n" . submit_button(__('Next'),'primary','next', false) . "</div>\n<div class=\"col2\">" . submit_button(__('Next'),'primary','next2', false) ."</div><div class=\"col2\">" . submit_button(__('Save'),'primary','save', false) . "</div>\n";
                echo "</div>\n";
        }
    }
}

function volaris_install() {
   global $wpdb;
   global $volaris_db_version;
   
   require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	//purge all tables related to volaris for clean start *TODO* DEVELOP A WAY TO SIMPLY CHECK FOR EXISTING TABLES
     $drop = "DROP TABLE `" .$wpdb->prefix ."CAMPAIGNS`, `" .$wpdb->prefix ."CAMPAIGN_RELATIONSHIPS`, `" .$wpdb->prefix ."CHARACTER`, `" .$wpdb->prefix ."CHAR_META`, `" .$wpdb->prefix ."GROUPS`, `" .$wpdb->prefix ."GROUP_RELATIONSHIPS`, `" .$wpdb->prefix ."ITEMS`, `" .$wpdb->prefix ."ITEM_META`, `" .$wpdb->prefix ."ITEM_RELATIONSHIPS`, `" .$wpdb->prefix ."PARTYS`, `" .$wpdb->prefix ."PARTY_RELATIONSHIPS`";
     $wpdb->query($drop);

   //begin creating tables - 11 total

   $table_character = $wpdb->prefix . "CHARACTER"; //this prepends the prefix set in wp-config.php
      
   $sql_character = "CREATE TABLE " . $table_character . " (
	`CHARID` BIGINT(20) NOT NULL AUTO_INCREMENT ,
	`NAME` VARCHAR(45) NULL ,
	`PLAYERID` BIGINT(20) NULL,
	PRIMARY KEY  (`CHARID`) )
	ENGINE = InnoDB;";  //the sql query to create the table
	 
   dbDelta($sql_character); //create the table, if exists check for updates to table i.e. new columns etc.
   
   //repeat the same structure to finish creating tables.
   
   $table_char_meta = $wpdb->prefix . "CHAR_META";
   
   $sql_char_meta = "CREATE TABLE " . $table_char_meta ." (
	`METAID` BIGINT(20) NOT NULL AUTO_INCREMENT ,
	`CHARID` BIGINT(20) NULL ,
	`META_KEY` VARCHAR(45) NULL ,
	`META_VALUE` VARCHAR(45) NULL ,
	PRIMARY KEY  (`METAID`) ,
	KEY `fk_CHARID` (`CHARID` ASC))
	ENGINE = InnoDB;";
	
     dbDelta($sql_char_meta);
	
	$table_item_relationships = $wpdb->prefix . "ITEM_RELATIONSHIPS";
	
	$sql_item_relationships = "CREATE TABLE " . $table_item_relationships . " (
	`RELID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`CHARID` BIGINT(20) NULL ,
	`ITEMID` BIGINT(20) NULL ,
	PRIMARY KEY  (`RELID`) ,
	KEY `fk_CHARID` (`CHARID` ASC))
	ENGINE = InnoDB;";

     dbDelta($sql_item_relationships);
   
   $table_party_relationships = $wpdb->prefix . "PARTY_RELATIONSHIPS";
   
   $sql_party_relationships = "CREATE TABLE ". $table_party_relationships . " (
	`RELID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`CHARID` BIGINT(20)  NULL ,
	`PARTYID` BIGINT(20)  NULL ,
	PRIMARY KEY  (`RELID`) ,
	KEY `fk_CHARID_CHARACTER` (`CHARID` ASC) ,
	KEY `fk_PARTYID_PARTY` (`PARTYID` ASC))
	ENGINE = InnoDB;";
	
	 dbDelta($sql_party_relationships);
	
	$table_items = $wpdb->prefix . "ITEMS";
	
	$sql_items = "CREATE TABLE " . $table_items . " (
	`ITEMID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`NAME` VARCHAR(45) NULL ,
	PRIMARY KEY  (`ITEMID`) ,
	KEY `ITEMID` (`ITEMID` ASC))
	ENGINE = InnoDB;";
	
	 dbDelta($sql_items);
	
	$table_group_relationships = $wpdb->prefix . "GROUP_RELATIONSHIPS";
	
	$sql_group_relationships = "CREATE TABLE " . $table_group_relationships . " (
	`RELID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`GID` BIGINT(20)  NULL ,
	`ITEMID` BIGINT(20) NULL ,
	PRIMARY KEY  (`RELID`) ,
	KEY `GID` (`GID` ASC))
	ENGINE = InnoDB;";
	
	 dbDelta($sql_group_relationships);
	
	$table_groups = $wpdb->prefix . "GROUPS";
	
	$sql_groups = "CREATE TABLE " . $table_groups . " (
	`GID` BIGINT(20) NOT NULL AUTO_INCREMENT ,
	`NAME` VARCHAR(45) NULL ,
	PRIMARY KEY  (`GID`) ,
	KEY `GID` (`GID` ASC))
	ENGINE = InnoDB;";
	
	 dbDelta($sql_groups);
	
	$table_item_meta = $wpdb->prefix . "ITEM_META";
	
	$sql_item_meta = "CREATE TABLE " . $table_item_meta . " (
	`METAID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`ITEMID` BIGINT(20) NULL ,
	`META_KEY` VARCHAR(45) NULL ,
	`META_VALUE` VARCHAR(45) NULL ,
	PRIMARY KEY  (`METAID`) ,
	KEY `fk_ITEM_META_ITEMS` (`ITEMID` ASC))
	ENGINE = InnoDB;";
	
	 dbDelta($sql_item_meta);
	
	$table_partys = $wpdb->prefix . "PARTYS";
	
	$sql_partys = "CREATE TABLE " . $table_partys . " (
	`PARTYID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`NAME` VARCHAR(45) NULL ,
	PRIMARY KEY  (`PARTYID`) ,
	KEY `PARTYID` (`PARTYID` ASC))
	ENGINE = InnoDB;";
	
	 dbDelta($sql_partys);
	
	$table_campaign_relationships = $wpdb->prefix . "CAMPAIGN_RELATIONSHIPS";
	
	$sql_campaign_relationships = "CREATE TABLE " . $table_campaign_relationships . " (
	`RELID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`CAMPAIGNID` BIGINT(20) NULL ,
	`PARTYID` BIGINT(20) NULL ,
	PRIMARY KEY  (`RELID`) ,
	KEY `PARTYID` (`PARTYID` ASC) ,
	KEY `CAMPAIGNID` (`CAMPAIGNID` ASC))
	ENGINE = InnoDB;";

	 dbDelta($sql_campaign_relationships);
	
	$table_campaigns = $wpdb->prefix . "CAMPAIGNS";
	
	$sql_campaigns = "CREATE TABLE " . $table_campaigns . " (
	`CAMPAIGNID` BIGINT(20)  NOT NULL AUTO_INCREMENT ,
	`NAME` VARCHAR(45) NULL ,
	`ID` BIGINT(20) NULL,
	PRIMARY KEY  (`CAMPAIGNID`) )
	ENGINE = InnoDB;";
	
	 dbDelta($sql_campaigns);
	
	volaris_install_data();
	
	add_option("volaris_db_version", $volaris_db_version);
}
register_activation_hook(__FILE__,'volaris_install');

function display_status() {
	echo "<p id=\"status\">volaris active</p>";
}
add_action('admin_notices','display_status');

function add_css() {
	// This makes sure that the positioning is also good for right-to-left languages
	$x = is_rtl() ? 'left' : 'right';

	echo "
        <script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\"></script>
        <script type=\"text/javascript\" src=\"". plugin_dir_url(__FILE__) ."js/buttons.js\"></script>
	<style type='text/css'>
	.alignright {
		text-align: right;
	}
	
	.aligncenter {
		text-align: center;
	}
	
	.bold {
		font-weight: bold;
		font-size: 12pt;
	}
	
	.border {
		border: 1px solid #000;
	}
	
	.shade-grey {
		background: #cccccc;
	}
	
	.shade-orange {
		background: orange;
	}
	
	#status {
		float: $x;
		padding-$x: 15px;
		padding-top: 5px;		
		margin: 0;
		font-size: 10px;
	}
                
         #charabilities, #charparty, #previous, #previous2, #next2, #save {
            display: none;
         }
	
	#wpbody fieldset.TableLike {
		margin-bottom: 15px;
	}
	
	#wpbody fieldset.TableLike .field {
		line-height: 2em;
		/* margin-left: 15%; */
		padding: 5px 0 2px 10px;
	}
	
	#wpbody fieldset.TableLike .fieldsmall {
		margin: 10px 0 0 0;
		vertical-align: center;
		text-align: center;
                line-height: 2em;
                display: block;
	}
	
	#wpbody fieldset.TableLike .col25 .current {
		margin: 0;
	}
	
	#wpbody fieldset.TableLike .fieldsmall input {
		width: 100%;
	}
		
	#wpbody fieldset.TableLike label {
		display: block;
		float: left;
		font-size: 9px;
		/* line-height: 1.2em; */
		margin-left: 10px;
		padding: 0 0 3px 0;
		font-style: italic;
		/* text-align: right; */
		width: 100%;
	}
	
	#wpbody fieldset.TableLike label.biglabel {
		padding: 3px;
		margin: 6px 6px 6px 10px;
		color: #FFFFFF;
		background-color: #000;
		text-align: center;
                font-size: 14pt;
		width: 80px;
                font-family: serif;
	}
        
    #wpbody fieldset.TableLike label.biglabel small {
                font-size: 7pt;
        }
	
	#wpbody fieldset.TableLike label.labelsmall {
		margin-left: 0;

/* 		width: 100%; */
		text-align: center;
	}
        
    #wpbody fieldset.TableLike .fieldsmall .button-primary {
		float: left;
		clear: none;
		width: auto;
    }
	
	#wpbody fieldset.TableLike .small-head {
		margin: 10px 0 0 5%;
		width: 100%;
		overflow: visible;
		position: relative;
	}
        
	#wpbody fieldset.TableLike .col05 {
		float: left;
		width: 5%;
		position: relative;
	}
        
	#wpbody fieldset.TableLike .col10 {
		float: left;
		width: 10%;
		position: relative;
	}

	#wpbody fieldset.TableLike .col17 {
		float: left;
		width: 17%;
		position: relative;
	}
        
	#wpbody fieldset.TableLike .col20 {
		float: left;
		width: 20%;
		position: relative;
	}

	#wpbody fieldset.TableLike .col25 {
		float: left;
		width: 25%;
		position: relative;
	}

	#wpbody fieldset.TableLike .col30 {
		float: left;
		width: 33%;
		position: relative;
	}
	
	#wpbody fieldset.TableLike .col4 {
		float: left;
		width: 40%;
		position: relative;
	}
	
	#wpbody  fieldset.TableLike .col5 {
		float: left;
		width: 50%;
		position: relative;
	}
                
    #wpbody  fieldset.TableLike .col6 {
		float: left;
		width: 60%;
		position: relative;
	}  
	
    #wpbody  fieldset.TableLike .col75 {
		float: left;
		width: 75%;
		position: relative;
	}
	
	#wpbody  fieldset.TableLike .col100 {
		float: left;
		width: 100%;
		position: relative;
	}	
	

	.TableLike .Field select, .TableLike .Field input, .TableLike .Field textarea {
		max-width: 100%;
	}
	
	.W10pc {
		width: 10%;
	}
	.W25pc {
		width: 25%;
	}
	.W65pc {
		width: 65%;
	}
	.W75pc {
		width: 75%;
	}
	.W100pc {
		width: 100%;
	}
	
	</style>
	";
}

add_action( 'admin_head', 'add_css' );

function volaris_install_data() {
   global $wpdb;
   
   $wpdb->insert($wpdb->prefix . "CAMPAIGNS", array('CAMPAIGNID' => 'NULL', 'NAME' => 'Journals from Volaris') );
   $wpdb->insert($wpdb->prefix . "PARTYS", array('PARTYID' => 'NULL', 'NAME' => 'volaris-main') );
   $wpdb->insert($wpdb->prefix . "CAMPAIGN_RELATIONSHIPS", array('RELID' => 'NULL', 'CAMPAIGNID' => '1', 'PARTYID' => '1') );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Llizia Darkheart",  ) );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Dawnsky" ) );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Umgrol" ) );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Montgomery" ) );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Kelsol" ) );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Ceytar" ) );
   $wpdb->insert($wpdb->prefix . "CHARACTER", array('CHARID' => 'NULL', 'NAME' => "Kaylynn" ) );
/*    $wpdb->insert($wpdb->prefix . "PARTY_RELATIONSHIPS", array('RELID' => 'NULL', 'CHARID' => '1', 'PARTYID' => '1') );
   $wpdb->insert($wpdb->prefix . "PARTY_RELATIONSHIPS", array('RELID' => 'NULL', 'CHARID' => '2', 'PARTYID' => '1') );
   $wpdb->insert($wpdb->prefix . "PARTY_RELATIONSHIPS", array('RELID' => 'NULL', 'CHARID' => '3', 'PARTYID' => '1') ); */
}

function plugin_page() {
	$menutitle = __('Volaris','volaris');
	$permission_needed = 'manage_options';
	add_menu_page('Volaris',$menutitle,$permission_needed,'volaris-settings','summary');
	add_submenu_page('volaris-settings',__('Summary','summary'),__('Summary','summary'),$permission_needed,'volaris-settings');
	add_submenu_page('volaris-settings',__('Characters','characters'),__('Characters','characters'),$permission_needed,'characters', array('Pages', 'character_page'));
	add_submenu_page('volaris-settings',__('Partys','partys'),__('Partys','partys'),$permission_needed,'partys','party_page');
	add_submenu_page('volaris-settings',__('Campaigns','campaigns'),__('Campaigns','campaigns'),$permission_needed,'campaign','campaign_page');
}
add_action('admin_menu','plugin_page');

function profile_page() {
	$menutitle = __('Volaris','volaris');
	$permission_needed = 'read';
	#add_menu_page('Volaris',$menutitle,$permission_needed,'profile.php','summary');
	$charactersheet = new CharacterSheet();
	add_submenu_page('profile.php',__('Character Sheet','character-sheet'),__('Character Sheet','character-sheet'),$permission_needed,'charactersheet',array($charactersheet, 'build_character_sheet'));
}
add_action('admin_menu','profile_page');

/**
 * Get all parties 
 * 
 * Uses global <code>$wpdb</code>
 * 
 * @return an array containing <code>partyid => partyname</code>
 */
function get_parties() {
    global $wpdb;
    
    $sql = "SELECT * FROM " . $wpdb->prefix . "PARTYS";
    $results = $wpdb->get_results($sql, ARRAY_A);
    
    return $results;
}
?>